
import { Component, sys, _decorator } from 'cc';
import { CharacterClass, constant } from '../constant';
import { GameApp } from '../GameApp';
const { ccclass, property } = _decorator;


/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
@ccclass
export class UserData extends Component {
    public static Instance: UserData = null as unknown as UserData
    private localDataStr: string = "TDGamelocalData"//本地数据

    public isInit: boolean = false

    public localData = {}




    onLoad() {
        if (UserData.Instance === null) {

            UserData.Instance = this;
        }
        else {
            this.destroy();
            return;
        }



    }



    Init() {


    }
    //获取当前角色类型
    public getPlayerType() {
        return this.getLocalData()["playerType"]
    }
    //获取当前玩家数据
    public getPlayerData() {

        return this.getLocalData()["playerConfig"][this.getPlayerType()]
    }

    public getLocalData() {
        if (localStorage.getItem(this.localDataStr)) {
            this.localData = JSON.parse(localStorage.getItem(this.localDataStr))
        } else {


            this.localData = {

                "friendlyData": {},
                "bulidData": {},
                "playerId": null,
                "playerName": null,
                "icon": null,

                "staticId": null,

                "level":1,


                // "nowLevel": 1,
                "score": 0,

                "gold": 800,//金币 800

                "ore": 500,//矿石   500

                "playerType": CharacterClass.Archer,//弓箭手信息
                //获取对应玩家的血量  hp[playerType] 

                "lastClaimTime": 0,//抖音领取奖励侧边栏
                "playerConfig": {

                    "Archer": {


                        "skill": {


                        }


                        //其他职业

                    },

                },




                "qiandao": [
                    {
                        flag: false,//今天是否签到
                        num: 1,//星期

                    },
                    {
                        flag: false,
                        num: 2,

                    },
                    {
                        flag: false,
                        num: 3,

                    },
                    {
                        flag: false,
                        num: 4,

                    },
                    {
                        flag: false,
                        num: 5,

                    },
                    {
                        flag: false,
                        num: 6,

                    },
                    {
                        flag: false,
                        num: 7,

                    }


                ],
            }
            //首次加载数据 同步skill
            const mySkill = constant.characterConfig
            for (let key in mySkill) {

                const m2 = mySkill[key]["config"]
                for (let key2 in m2) {
                    this.localData["playerConfig"][key]["skill"][key2] = m2[key2]["level"]
                }

            }
            const build = constant.buildData
            for (let key in build) {

                const level = build[key]["level"]
                this.localData["bulidData"][key] = level


            }

            const friendly = constant.friendlyData
            for (let key in friendly) {

                this.localData["friendlyData"][key] = {
                    level: friendly[key]["level"],
                    isBuy: friendly[key]["isBuy"],

                }


            }



            console.log("首次最终技能数据", this.localData);

            this.saveLocalData()
        }


        return this.localData
    }
    //存储用户数据到本地
    public saveLocalData() {
        localStorage.setItem(this.localDataStr, JSON.stringify(this.localData));
        //同步到常量里面
        GameApp.Instance.synchronousData()


    }

    //无需同步大表
    public saveLocalData2() {
        localStorage.setItem(this.localDataStr, JSON.stringify(this.localData));




    }

    start() {

    }

    update(deltaTime: number) {

    }
}


